﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using AutoMapper;
using Berico.SnagL.Host.Facebook.Models;
using SnagL.FacebookHostDemo.GraphML;

namespace Berico.SnagL.Host.Facebook
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            ConfigureMapper();
        }

        /// <summary>
        /// Initializes the mapper
        /// </summary>
        private static void ConfigureMapper()
        {
            Mapper.CreateMap<FacebookUser, Node>()
                .ConstructUsing(e =>
                {
                    Node node = new Node
                    {
                        NodeId = e.Name
                    };

                    // Name
                    node.Data.Add(new Data
                    {
                        Key = "node-attr-Name-desc",
                        InnerText = "{\"Name\":\"Name\",\"PreferredSimilarityMeasure\":\"\",\"SemanticType\":0,\"Visible\":true}"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-attr-Name-val",
                        InnerText = e.Name
                    });

                    // FirstName
                    node.Data.Add(new Data
                    {
                        Key = "node-attr-FirstName-desc",
                        InnerText = "{\"FirstName\":\"FirstName\",\"PreferredSimilarityMeasure\":\"\",\"SemanticType\":0,\"Visible\":true}"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-attr-FirstName-val",
                        InnerText = e.FirstName
                    });

                    // LastName
                    node.Data.Add(new Data
                    {
                        Key = "node-attr-LastName-desc",
                        InnerText = "{\"LastName\":\"LastName\",\"PreferredSimilarityMeasure\":\"\",\"SemanticType\":0,\"Visible\":true}"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-attr-LastName-val",
                        InnerText = e.LastName
                    });

                    // Birthday
                    node.Data.Add(new Data
                    {
                        Key = "node-attr-Birthday-desc",
                        InnerText = "{\"Birthday\":\"Birthday\",\"PreferredSimilarityMeasure\":\"\",\"SemanticType\":0,\"Visible\":true}"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-attr-Birthday-val",
                        InnerText = e.Birthday ?? "Hidden By Permission"
                    });

                    // Hometown
                    node.Data.Add(new Data
                    {
                        Key = "node-attr-Hometown-desc",
                        InnerText = "{\"Hometown\":\"Hometown\",\"PreferredSimilarityMeasure\":\"\",\"SemanticType\":0,\"Visible\":true}"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-attr-Hometown-val",
                        InnerText = e.Hometown != null ? e.Hometown.Name : "Hidden By Permission"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-Description",
                        InnerText = "Generated by SnagLFacebookHostDemo"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-DisplayValue",
                        InnerText = e.Name
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-ImageSource",
                        InnerText = "/Berico.SnagL;component/Resources/Icons/person.png"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-Position",
                        InnerText = "0,0"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-IsHidden",
                        InnerText = "False"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-BackgroundColor",
                        InnerText = "#ff740000"
                    });

                    node.Data.Add(new Data
                    {
                        Key = "node-prop-SelectionColor",
                        InnerText = "#ff4a75a9"
                    });

                    return node;
                });
        }
    }
}